import java.util.Random;

public class Practice2 {
    //手写一下快排, 老是忘
    public static void quickSort(int[] nums,int left,int right){
        if(left >= right){
            return;
        }
        //数组分三块
        //[left,l] [l,r-1] [r,right]
        int l = left - 1;
        int r = right + 1;
        int i = left;
        int key = nums[new Random().nextInt(right - left + 1) + left];
        while(i < r){
            if(nums[i] < key){
                swap(nums,++l,i++);
            }else if(nums[i] == key){
                i++;
            }else{
                swap(nums,--r,i);
            }
        }
        quickSort(nums,left,l);
        quickSort(nums,r,right);
    }

    private static void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

    public static void main(String[] args) {
        int[] nums = {4,65,87,787,8,78,67,6,56,45,43,4,3,43};
        quickSort(nums,0,nums.length - 1);
        System.out.println();
    }
}
